package com.atguigu.gmall.payment.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.atguigu.gmall.payment.service.ZfbPayService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Map;

/***
 * 支付宝支付的service层的接口的实现类
 */
@Service
public class ZfbPayServiceImpl implements ZfbPayService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private AlipayClient alipayClient;

    @Autowired
    private AlipayTradePagePayRequest pagePayRequest;

    @Autowired
    private AlipayTradeQueryRequest tradeQueryRequest;


    /**
     * 获取支付宝支付页面
     *
     * @param body
     * @param orderId
     * @param amount
     * @return
     */
    @Override
    public String getPayPage(String body, String orderId, String amount) {
        /******必传参数******/
        JSONObject bizContent = new JSONObject();
        //商户订单号，商家自定义，保持唯一性
        bizContent.put("out_trade_no", orderId);
        //支付金额，最小值0.01元
        bizContent.put("total_amount", amount);
        //订单标题，不可使用特殊符号
        bizContent.put("subject", body);
        //电脑网站支付场景固定传值FAST_INSTANT_TRADE_PAY
        bizContent.put("product_code", "FAST_INSTANT_TRADE_PAY");
        //设置请求参数的集合
        pagePayRequest.setBizContent(bizContent.toString());
        try {
            //发送请求
            AlipayTradePagePayResponse response = alipayClient.pageExecute(pagePayRequest);
            if (response.isSuccess()) {
                //发送成功，返回结果
                return response.getBody();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 查询订单的支付情况
     *
     * @return
     */
    @Override
    public String getPayResultMap(String orderId) {
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", orderId);
        //设置请求参数
        tradeQueryRequest.setBizContent(bizContent.toString());
        try{
            //发送请求
            AlipayTradeQueryResponse response = alipayClient.execute(tradeQueryRequest);
            if(response.isSuccess()){
                //请求成功，返回结果
                return response.getBody();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 收到回调通知，发送消息修改订单
     *
     * @param callbackNotifyJsonString
     */
    @Override
    public void callbackNotify(String callbackNotifyJsonString) {
        //1 发送消息
        rabbitTemplate.convertAndSend("order_pay_exchange","order.normal.pay",callbackNotifyJsonString);
    }
}
